package thread_01;

class Counter{
    private int count = 0;

    public void add(){
        count++;
    }

    synchronized public void add2(){
        count++;
    }

    synchronized public static void add3(){
//        synchronized修饰static方法，相当于针对该 类的 对象 加锁
//        相当于
//        static void func（）{
//         synchronized（Counter.class）{
//        ....
//        }
    }

    public int get(){
        return count;
    }
        }
public class Demo20 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
//        Object locker = new Object();

        Thread t1 = new Thread(() ->{
            for (int i = 0; i < 50000; i++) {
                counter.add2();
            }
        });
        Thread t2 = new Thread(() ->{
            for (int i = 0; i < 50000; i++) {
//                synchronized (locker){
//                    counter.add();
//                }
                counter.add2();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count=" + counter.get());
    }
}
